package com.xiaxueqi.web.home;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.security.auth.exception.AuthException;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.CRUDActionSupport;
import com.octo.captcha.service.CaptchaService;
import com.octo.captcha.service.CaptchaServiceException;
import com.xiaxueqi.entity.security.AppUserDetail;

@Result(name = CRUDActionSupport.ERROR, location = "/common/403.jsp", type = "redirect")
public class UserDetailAction extends CRUDActionSupport<AppUserDetail> {
	private static final long serialVersionUID = 1L;

	@Autowired
	private BaseService baseService;
	private AppUserDetail entity;
	private Integer id;
	private UserDetail detail = SecurityContext.getUserDetail();
	private Integer userId = detail != null ? Integer.valueOf(detail
			.getUserId().intValue()) : null;

	private String captcha;
	private CaptchaService captchaService;
	private String captchaError;

	public String getCaptchaError() {
		return captchaError;
	}

	public CaptchaService getCaptchaService() {
		return captchaService;
	}

	public void setCaptchaService(CaptchaService captchaService) {
		this.captchaService = captchaService;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	@Override
	public String delete() throws Exception {
		System.out.println("delete()");
		return RELOAD;
	}

	@Override
	public String list() throws Exception {
		System.out.println("list()");
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		this.id = userId;
		if (this.id != null) {
			this.entity = this.baseService.get(AppUserDetail.class, id);
		} else {
			this.entity = new AppUserDetail();
		}
		System.out.println("prepareModel()");
	}

	@Override
	public String save() throws Exception {
		HttpServletRequest request = SecurityContext.getRequest();
		String captchaID = request.getSession().getId();
		boolean success = false;
		try {
			success = captchaService.validateResponseForID(captchaID, captcha);
		} catch (CaptchaServiceException e) {
			logger.error(e.getMessage(), e);
			throw new AuthException(e);
		}
		if (!success) {
			this.captchaError = "error";
			// return 一个没有定义的跳转"stop"，将只将本页面刷新
			return "stop";
		} else {
			baseService.update(entity);
			addActionMessage("保存成功");
			return RELOAD;
		}
	}

	public AppUserDetail getModel() {
		System.out.println("getModel()");
		return entity;
	}

	@Override
	public String input() throws Exception {
		System.out.println("input()");
		return INPUT;
	}

	public String email() throws Exception {
		return "email";
	}

	public String password() throws Exception {
		return "password";
	}

}
